package com.freesky.biz.rights.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.RightsActivityEntity;
import com.freesky.biz.rights.service.IRightsActivityService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.QueryWrapperUtils;
import com.freesky.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 权益活动管理
 */
@RestController
@RequestMapping("/rights/activity")
public class RightsActivityController extends BaseController {

    @Autowired
    private IRightsActivityService rightsActivityService;

    /**
     * 分页获取权益活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activity:list')")
    @GetMapping("/page")
    public TableDataInfo page(RightsActivityEntity rightsActivity) {
        QueryWrapper<RightsActivityEntity> queryWrapper = QueryWrapperUtils.toSimpleQuery(rightsActivity);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByDesc("create_time");
        IPage<RightsActivityEntity> pageList = rightsActivityService.page(plusPage(),queryWrapper);
        return getDataTable(pageList);
    }

    @PreAuthorize("@ss.hasPermi('rights:activity:list')")
    @GetMapping("/list")
    public AjaxResult list(RightsActivityEntity rightsActivity) {
        QueryWrapper<RightsActivityEntity> queryWrapper = QueryWrapperUtils.toSimpleQuery(rightsActivity);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByDesc("create_time");
        return AjaxResult.success(rightsActivityService.list(queryWrapper));
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return success(rightsActivityService.getById(id));
    }

    /**
     * 新增权益活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activity:add')")
    @Log(title = "权益活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RightsActivityEntity rightsActivity) {
        rightsActivity.setDelFlag("0");
        rightsActivity.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsActivity.setCreateTime(new Date());
        return toAjax(rightsActivityService.save(rightsActivity));
    }

    /**
     * 修改权益活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activity:edit')")
    @Log(title = "权益活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RightsActivityEntity rightsActivity) {
        RightsActivityEntity queryData = rightsActivityService.getById(rightsActivity.getId());
        if(queryData == null){
            return AjaxResult.success("权益活动信息不存在，无法修改!");
        }
        rightsActivity.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
        rightsActivity.setUpdateTime(new Date());
        return toAjax(rightsActivityService.updateById(rightsActivity));
    }

    /**
     * 删除权益活动
     */
    @PreAuthorize("@ss.hasPermi('rights:activity:remove')")
    @Log(title = "权益活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(rightsActivityService.deleteRightsActivityByIds(ids.toArray(new String[0])));
    }
}
